package com.liam.apppush.client.oppo;

import com.alibaba.fastjson.JSONObject;
import com.liam.apppush.config.OPPOConfig;
import com.liam.apppush.util.HttpUtil;
import com.liam.apppush.util.Sha256Util;
import com.oppo.push.server.Notification;
import com.oppo.push.server.Result;
import com.oppo.push.server.Sender;
import com.oppo.push.server.Target;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * sdk  不包含全部
 *
 *  可以发送 当个，批量。全量。
 *  标签只能一个一个加
 *
 */
@Slf4j
@Component
public class OPPOClient extends OPPOConfig {

    public static final String OPPO_BASE = "https://api.push.oppomobile.com/";
    public static final String BASE = "https://api-device.push.heytapmobi.com/";

    public static final String AUTH_URL = OPPO_BASE + "/server/v1/auth";

    public static final String ADD_TAG_URL = BASE + "/server/v1/device/add_tags";
    public static final String SUBSCRIBE_TAG_URL = BASE + "/server/v1/device/subscribe_tags";
    public static final String UNSUBSCRIBE_TAG_URL = BASE + "/server/v1/device/unsubscribe_tags";
    public static final String SMALL_PIC_URL = BASE + "/server/v1/device/small_picture";


    /**
     * 获取 token
     *
     * @return
     * @throws IOException
     */
    public String getAccessToken() throws IOException {
        String time = System.currentTimeMillis() + "";
        String sign = Sha256Util.getSHA256StrJava(appKey+time+masterSecret);
        JSONObject param = new JSONObject();
        param.put("sign",sign);
        param.put("timestamp",time);
        param.put("app_key",appKey);

        JSONObject result = HttpUtil.httpPost(AUTH_URL,param);
        if(result != null && result.getInteger("code") == 0){
            // 默认一天 86400
            return result.getJSONObject("data").getString("auth_token");
        }
        return null;
    }


    /**
     * 添加标签
     * @throws Exception
     */
    public void addTag() throws Exception {
        JSONObject param = new JSONObject();
        param.put("name","标签名字长度50字符");
        param.put("desc","标签描述,字符长度200字符");

        Map<String,String> heads = new HashMap<>();
        heads.put("auth_token","c9ac9394dc24b1b380d0045814e86427c361aabc2fb77d9323f4006ab4d7d232");

        JSONObject result = HttpUtil.httpPost(ADD_TAG_URL,heads,param);
    }

    /**
     * 订阅标签
     * @throws Exception
     */
    public void subscribeTags() throws Exception {
        JSONObject param = new JSONObject();
        param.put("registration_id","cid");

        /**
         * 标签内容,多个标签之间使用英文分号分隔，单个标签字节长度50字符；
         * 限制：单次调用接口可设置50个标签；单个用户总量上限也是50个标签
         * 这个接口可重复调用，多次调用不会清理原来设置的标签
         */
        param.put("tags","a;b;c");

        Map<String,String> heads = new HashMap<>();
        heads.put("auth_token","c9ac9394dc24b1b380d0045814e86427c361aabc2fb77d9323f4006ab4d7d232");

        JSONObject result = HttpUtil.httpPost(SUBSCRIBE_TAG_URL,heads,param);
    }

    /**
     * 取消订阅
     * @throws Exception
     */
    public void unSubscribeTags() throws Exception {
        JSONObject param = new JSONObject();
        param.put("registration_id","cid");

        /**
         * 标签内容,多个标签之间使用英文分号分隔，单个标签字节长度50字符；
         * 限制：单次调用接口可设置50个标签；单个用户总量上限也是50个标签
         * 这个接口可重复调用，多次调用不会清理原来设置的标签
         */
        param.put("tags","a;b;c");

        Map<String,String> heads = new HashMap<>();
        heads.put("auth_token","c9ac9394dc24b1b380d0045814e86427c361aabc2fb77d9323f4006ab4d7d232");

        JSONObject result = HttpUtil.httpPost(UNSUBSCRIBE_TAG_URL,heads,param);
    }


    /**
     * 上传小图
     * @param file
     * @throws Exception
     */
    public void smallPic(MultipartFile file) throws Exception {
        JSONObject param = new JSONObject();
        param.put("picture_ttl",86400);//图片保存的存活时间(time_to_live) (单位：秒)，最长30天

        Map<String,String> heads = new HashMap<>();
        heads.put("auth_token","c9ac9394dc24b1b380d0045814e86427c361aabc2fb77d9323f4006ab4d7d232");

        JSONObject result = HttpUtil.httpPost(SMALL_PIC_URL,param,file.getName(),file);
    }
}
